package org.liang.delete;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;
import java.util.stream.Collectors;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class JFrameRun {
	private  JTextField pathFiled = new JTextField(20);
	JFrame frame = new JFrame("删除maven仓库update文件");
	JPanel panel1 = new JPanel();
	JTextArea area = new JTextArea();
	public void show() {

		JButton button = new JButton("删除");
		JButton descBtn = new JButton("描述");
		button.addActionListener(new DeleteActionListener());
		descBtn.addActionListener(new DescActionListener());
		pathFiled.setText("D:\\.m2\\repository");
		
		panel1.add(pathFiled);
		panel1.add(button);
		panel1.add(descBtn);
		JScrollPane scrollPane =new JScrollPane(area);
		scrollPane.setSize(400, 200);
		frame.add("North",panel1);
		frame.add(scrollPane);

		frame.setSize(400, 200);
		frame.setLocationRelativeTo(frame.getOwner());//居中效果
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}

	class DescActionListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			StringBuffer sb =new StringBuffer();
			sb.append("由于网络原因，maven在更新jar包时，会导致下载失败，\n");
			sb.append("出现xxx.jar.lastUpdated文件，maven再次maven update，依然不生效。\n");
			sb.append("需要先把仓库中的xxx.jar.lastUpdated文件及上层文件夹删除，才可以继续进行maven-update\n");
			sb.append("删除规则：删除文件夹下包含xxx.jar.lastUpdated的文件夹。通过递归扫描给定目录下的所有目录");
			JOptionPane.showMessageDialog(frame, sb.toString(), "消息",JOptionPane.WARNING_MESSAGE);
		}
		
	}
	class DeleteActionListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
				String path = pathFiled.getText();
				if(path == null || path.trim().equals("")) {
					JOptionPane.showMessageDialog(frame, "路径不能为空", "消息",JOptionPane.WARNING_MESSAGE); 
					return;
				}
				try {
					File f = new File(path);
					if(!f.exists()) {
						JOptionPane.showMessageDialog(frame, "路径不存在", "消息",JOptionPane.WARNING_MESSAGE); 
						return;
					}
					DeleteMavenUpdate.delDirs.clear();
					area.setText("");
					DeleteMavenUpdate.deleteDir(f);
					List<String> delDirs = DeleteMavenUpdate.delDirs;
					String str = delDirs.stream().collect(Collectors.joining("\n"));
					System.out.println(str);
					area.setText(str);
					
					
					
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(frame, "系统异常", "消息",JOptionPane.WARNING_MESSAGE); 
					e1.printStackTrace();
				}
				JOptionPane.showMessageDialog(frame, "处理完成", "消息",JOptionPane.INFORMATION_MESSAGE); 
			
		}
		
	}
}
